package com.bolt.admin.module.sys.controller;

import com.bolt.admin.module.sys.service.OnlineUserService;
import com.bolt.admin.security.shiro.AuthContextHolder;
import com.bolt.admin.security.shiro.AuthUserDetails;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Set;


@RestController
@RequestMapping("/api/sys/online")
@RequiredArgsConstructor
@Api(tags = "在线用户管理")
public class OnlineController {

    private final OnlineUserService onlineUserService;


    @ApiOperation("查询在线用户")
    @GetMapping(value = "/page")
    public ResultMessage getOrgList(Pageable pageable, String userName) {
        return Results.success(onlineUserService.getAll(userName, pageable));
    }

    /**
     * 踢出用户
     *
     * @param keys
     * @return
     */
    @PostMapping(value = "kickOut")
    @RequiresPermissions("monitor:online,del")
    public ResultMessage kickOut(@RequestBody Set<String> keys) {
        AuthUserDetails authUserDetails = AuthContextHolder.getAuthUserDetails();
        if (keys.contains(authUserDetails.getKey())) {
            return Results.invalid("当前登录用户无法强退");
        }
        for (String key : keys) {
            onlineUserService.logOut(key);
        }
        return Results.success();
    }
}

 